package edu.umd.fosite.util;

/**
 * A <code>LoopTask</code> is a {@link Runnable} that executes another in an
 * infinite loop, at a fixed interval.
 * 
 */
public class LoopTask implements Runnable {

	long interval;
	Runnable task;

	public LoopTask(Runnable task, long millis) {
		interval = millis;
		this.task = task;
	}

	public void run() {
		long lastRun = System.currentTimeMillis();
		while (true) {
			task.run();
			try {
				long t = System.currentTimeMillis();
				long s = interval - (t - lastRun);
				if (s > 0)
					Thread.sleep(s);
				lastRun = t;
			} catch (InterruptedException e) {
				// TODO
			}
		}
	}

}
